<template>
  <div class="login-page" ref="loginWrapper">
    <div class="login-page__container">
      <div class="login-page__form-wrapper">
        <div style="display: flex; align-items: center; width: 500px">
          <div class="login-page__logo">
            <img
              src="@/assets/login-logo.png"
              alt="WhereToKnow Logo"
              class="login-page__logo-img"
            />
          </div>

          <h1 class="login-page__title">
            <img
              src="@/assets/word3.png"
              alt=""
              style="width: 100%; height: 100%"
            />
          </h1>
          <!-- <p class="login-page__subtitle">Hotel Management System</p> -->
        </div>

        <div class="login-page__form card">
          <div class="login-page__form-content">
            <p class="login-page__welcome-message">
              Log in to start your session
            </p>

            <form @submit.prevent="logins" class="login-form">
              <div class="login-form__group">
                <div class="login-form__input-container">
                  <div class="login-form__input-group">
                    <div class="login-form__input-icon">
                      <i class="fas fa-user"></i>
                    </div>
                    <input
                      type="text"
                      class="login-form__input"
                      placeholder="Username"
                      v-model="username"
                      ref="usernameInput"
                      @focus="handleFocus"
                      @blur="focusUsername = false"
                    />
                  </div>
                </div>
              </div>

              <div class="login-form__group">
                <div class="login-form__input-container">
                  <div class="login-form__input-group">
                    <div class="login-form__input-icon">
                      <i class="fas fa-lock"></i>
                    </div>
                    <input
                      type="password"
                      class="login-form__input"
                      placeholder="Password"
                      v-model="password"
                      ref="passwordInput"
                      @focus="handleFocus"
                      @blur="focusPassword = false"
                    />
                  </div>
                </div>
              </div>

              <div class="login-form__remember">
                <label class="login-form__remember-label">
                  <input
                    type="checkbox"
                    id="remember"
                    v-model="rememberMe"
                    class="login-form__remember-checkbox"
                  />
                  <span class="login-form__remember-custom"></span>
                  <span class="login-form__remember-text"> Remember me</span>
                </label>
              </div>

              <button
                type="submit"
                class="login-form__submit-btn btn primary"
                :class="{ 'login-form__submit-btn--loading': isLoading }"
                :disabled="isLoading"
              >
                <span v-if="!isLoading" class="login-form__submit-text"
                  >Log in</span
                >
                <span v-else class="login-form__loading-spinner"
                  >Loading...</span
                >
              </button>
            </form>

            <div class="login-page__forgot-password">
              <a href="#" class="login-page__forgot-link">
                Forget your password?</a
              >
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { goLogin,  } from "@/api/information.js";
import router from "@/router";
import { mapActions } from "vuex";

export default {
  name: "LoginView",
  data() {
    return {
      username: "",
      password: "",
      rememberMe: false,
      focusUsername: false,
      focusPassword: false,
      isLoading: false,
    };
  },
  methods: {
    ...mapActions("auth", ["login"]),
    handleFocus(event) {
      event.preventDefault();
      event.stopPropagation();
    },
    async logins() {
      if (!this.username || !this.password) {
        this.$message.error("Please enter your username and password");
        return;
      }

      this.isLoading = true;

      try {
        const res = await goLogin({
          email: this.username,
          password: this.password,
        });

        if (res.token) {
          console.log("收到Token:", res.token);

          if (this.rememberMe) {
            localStorage.setItem("token", res.token);
          } else {
            sessionStorage.setItem("token", res.token);
          }

          await this.login({
            token: res.token,
            userInfo: res.userInfo || null,
            rememberMe: this.rememberMe,
          });

          // const redirect = this.$route.query.redirect || "/";
          // router.push(redirect);

          router.push("/select-hotel");
          this.$message.success("login successfully");
        } else {
          this.$message.error(
            "Login failed. The username or password is incorrect"
          );
        }
      } catch (error) {
        this.$message.error("The login request failed. Please try again later");
      } finally {
        this.isLoading = false;
      }
    },
  },

};
</script>

<style lang="scss" scoped>
/* 基础样式重置 */
* {
  font-family: "Inter Variable", sans-serif;
  padding: 0;
  box-sizing: border-box;
}

.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 20px;

  &__container {
    max-width: 400px;
    width: 100%;
    z-index: 10;
  }

  &__form-wrapper {
    position: relative;
  }

  &__brand {
    display: flex;
    // flex-direction: column;
    align-items: center;
  }

  &__logo {
    width: 120px;
    height: 120px;
  }

  &__logo-img {
    width: 100%;
    height: 100%;
  }

  &__header {
    text-align: center;
    // margin-bottom: 30px;
    // padding: 0 20px;
  }

  &__title {
    width: 278px;
    // height: 150px;
    font-size: 28px;
    font-weight: 700;
    color: #1e40af;
    margin-left: 20px;
    // margin-bottom: 10px;
    letter-spacing: -0.5px;
  }

  &__subtitle {
    font-size: 14px;
    color: #64748b;
    font-weight: 400;
  }

  &__form {
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1),
      0 8px 10px -6px rgba(0, 0, 0, 0.05);
    overflow: hidden;
  }

  &__form-content {
    padding: 30px;
  }

  &__welcome-message {
    text-align: center;
    font-size: 16px;
    color: #475569;
    margin-bottom: 25px;
    font-weight: 500;
  }

  &__forgot-password {
    text-align: center;
    margin-top: 20px;
  }

  &__forgot-link {
    color: #3b82f6;
    text-decoration: none;
    font-size: 14px;
    transition: color 0.2s ease;

    &:hover {
      color: #2563eb;
      text-decoration: underline;
    }
  }
}

.login-form {
  &__group {
    margin-bottom: 20px;
  }

  &__input-container {
    display: block;
    width: 100%;
    height: 54px;
  }

  &__input-group {
    position: relative;
    width: 100%;
    height: 100%;
  }

  &__input-icon {
    position: absolute;
    left: 15px;
    top: 50%;
    transform: translateY(-50%);
    color: #94a3b8;
    font-size: 18px;
    z-index: 1;
  }

  &__input {
    width: 100%;
    height: 100%;
    padding: 14px 14px 14px 10px;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    font-size: 15px;
    color: #1f2937;
    background-color: #fff;
    transition: all 0.2s ease;
    outline: none;
    box-shadow: 0 0 0 0 rgba(59, 130, 246, 0);

    &:focus {
      border-color: #3b82f6;
      box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
    }
  }

  &__remember {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 25px;
  }

  &__remember-label {
    display: flex;
    align-items: center;
    cursor: pointer;
  }

  &__remember-checkbox {
    position: absolute;
    opacity: 0;
    cursor: pointer;
    height: 0;
    width: 0;
  }

  &__remember-custom {
    position: relative;
    height: 18px;
    width: 18px;
    background-color: #e5e7eb;
    border-radius: 4px;
    margin-right: 8px;
    transition: all 0.2s ease;
    border: 1px solid #d1d5db;
  }

  &__remember-checkbox:checked ~ &__remember-custom {
    background-color: #3b82f6;
    border-color: #3b82f6;
  }

  &__remember-custom:after {
    content: "";
    position: absolute;
    display: none;
    left: 6px;
    top: 3px;
    width: 5px;
    height: 10px;
    border: solid white;
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
  }

  &__remember-checkbox:checked ~ &__remember-custom:after {
    display: block;
  }

  &__remember-text {
    font-size: 14px;
  }

  &__submit-btn {
    width: 100%;
    padding: 14px;
    background-color: #3b82f6;
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    justify-content: center;
    align-items: center;

    &:hover {
      background-color: #2563eb;
      transform: translateY(-1px);
      box-shadow: 0 4px 6px -1px rgba(59, 130, 246, 0.2),
        0 2px 4px -1px rgba(59, 130, 246, 0.1);
    }

    &--loading {
      cursor: progress;
    }
  }

  &__loading-spinner {
    display: flex;
    align-items: center;
    justify-content: center;

    &:after {
      content: "";
      display: inline-block;
      width: 16px;
      height: 16px;
      margin-left: 8px;
      border: 2px solid rgba(255, 255, 255, 0.3);
      border-top-color: white;
      border-radius: 50%;
      animation: spin 1s ease-in-out infinite;
    }
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* 响应式设计 */
@media (max-width: 420px) {
  .login-page {
    &__form-content {
      padding: 25px;
    }

    &__title {
      font-size: 24px;
    }
  }

  .login-form {
    &__input-container {
      height: 50px;
    }
  }
}/* 小屏幕响应式调整（≤500px）- 保持图片尺寸，优化间距缓解拥挤 */
@media (max-width: 500px) {
  /* 头部区域 - 保持图片尺寸和居中，增加与表单的间距 */
  .login-page__form-wrapper > div {
    margin: 0 auto 30px !important; /* 增加底部留白，与表单分隔更清晰 */
    justify-content: center !important;
    display: flex !important;
    align-items: center !important;
    white-space: nowrap !important;
    width: auto !important;
    max-width: 94vw !important;
  }

  /* 保持Logo和Title尺寸不变（不修改宽高属性） */
  .login-page__logo {
    /* 仅保留原有尺寸设置，不做调整 */
    width: clamp(85px, 22vw, 120px) !important;
    height: auto !important;
    flex-shrink: 0 !important;
  }

  .login-page__title {
    /* 仅保留原有尺寸设置，不做调整 */
    width: clamp(200px, 60vw, 278px) !important;
    margin-left: 15px !important;
    flex-shrink: 0 !important;
  }

  /* 表单区域 - 增加内边距和留白 */
  .login-page__form {
    width: 100% !important;
    max-width: 90vw !important; /* 适当收窄表单宽度，增加左右留白 */
    margin: 0 auto !important; /* 居中显示 */
    padding: 0 5px !important;
  }

  .login-page__form-content {
    padding: 30px 15px !important; /* 增加上下内边距，内部更宽松 */
  }

  /* 输入框区域 - 增加间距 */
  .login-form__group {
    margin-bottom: 25px !important; /* 加大输入框之间的距离 */
  }

  .login-form__input-container {
    width: 100% !important;
    height: 54px !important;
    max-width: 400px !important;
    margin: 0 auto !important;
  }

  .login-form__input {
    padding: 16px 16px 16px 45px !important; /* 增加输入框内部留白，文字不拥挤 */
    font-size: 15px !important;
  }

  /* 记住密码区域 - 增加与按钮的间距 */
  .login-form__remember {
    margin-bottom: 30px !important; /* 加大与下方按钮的距离 */
    padding: 0 10px !important;
  }

  /* 按钮区域 - 增加点击区域和留白 */
  .login-form__submit-btn {
    padding: 16px 16px !important; /* 加大按钮内边距，视觉更舒展 */
    margin-bottom: 15px !important; /* 增加按钮底部留白 */
  }

  /* 忘记密码链接 - 增加顶部间距 */
  .login-page__forgot-password {
    margin-top: 25px !important; /* 与按钮拉开距离 */
  }
}

/* 超小屏幕（≤360px）优化 - 进一步增加留白 */
@media (max-width: 360px) {
  .login-page__form-content {
    padding: 30px 12px !important;
  }

  .login-form__group {
    margin-bottom: 22px !important;
  }

  .login-form__remember {
    margin-bottom: 28px !important;
  }
}
</style>
